package org.example.springblog.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.springblog.constants.Constant;
import org.example.springblog.model.BlogInfo;
import org.example.springblog.service.BlogService;
import org.example.springblog.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    private BlogService blogService;
    @RequestMapping("/getList")
    public List<BlogInfo> queryBlogList() {
        return blogService.queryBlogList();
    }
    @RequestMapping("/getBlogDetail")
    public BlogInfo getBlogDetail( Integer blogId,HttpServletRequest request) throws Exception {
        //后续再实现一个编辑和更新博客的操作
        //这里写的原因是因为一起写了，然后将作者和用户是不是一个id
        //返回到前端，进行判断
        log.info("getBlogDetail, 接收参数blogId:"+blogId);
        BlogInfo blogInfo=blogService.queryBlogById(blogId);
        //拿到登录用户的id
        String user_token=request.getHeader(Constant.USER_TOKEN_HEADER);
        Integer userId= JWTUtils.getIdFromToken(user_token);
        //进行判断id是否一样
        if(userId!=null && userId==blogInfo.getUserId()){
            blogInfo.setIsLoginUser(true);
        } else {
            blogInfo.setIsLoginUser(false);
        }
        log.info("queryBlogDetail, 接收参数:{}, 返回结果{}", blogId, blogInfo);
        return blogInfo;
    }
    @RequestMapping("/add")
    //在添加的时候，能够拿到这个用户信息
    //通过token
    public Boolean publishBlog(String title, String content, HttpServletRequest request) throws Exception {
        log.info("publishBlog, 接收参数: title:{},content:{}",title, content);
        if(!StringUtils.hasLength(title) || !StringUtils.hasLength(content)) {
            log.error("title or content 为空");
            return false;
        }
        String token=request.getHeader(Constant.USER_TOKEN_HEADER);
        Integer userId= JWTUtils.getIdFromToken(token);
        if(userId<0) {
            log.error("添加博客的时候，用户未登录");
            return false;
        }
        BlogInfo blogInfo=new BlogInfo(title,content,userId);
        //开始向数据库中添加信息
        Integer integer=blogService.insertInfo(blogInfo);
        if(integer<0) {
            log.error("添加博客失败");
            return false;
        }
        return true;
    }
    @RequestMapping("/update")
    public Boolean update(Integer blogId, String title, String content){
        log.info("博客更新update, blogId:"+blogId+", title:"+title+", content:"+content);
        if(!StringUtils.hasLength(title) || !StringUtils.hasLength(content)|| blogId<0) {
            log.error("更新博客，参数错误");
            return false;
        }
        BlogInfo blogInfo=new BlogInfo(title,content,blogId);
        Integer integer = blogService.updateBlog(blogInfo);
        if(integer<1) {
            log.error("更新博客失败");
            return false;
        }
        return true;
    }
    @RequestMapping("/delete")
    public Boolean deleteBlog(Integer blogId) {
        //这里删除还是走的是一个更新的操作
        BlogInfo blogInfo=new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);
        Integer integer = blogService.updateBlog(blogInfo);
        if(integer<1) {
            log.error("删除博客失败");
            return false;
        }
        return true;
    }
}
